<script src="/library/jquery-nestable/jquery.nestable.min.js"></script>
<div id="{{ id }}" class="dd dd-draghandle">
	{% if title_display is empty %} {% set title_display = false %} {%
	endif %} {% if title == null AND title_display == true %}
	<h4 class="header smaller lighter grey">{{ title }}</h4>
	<div class="space-8"></div>
	{% endif %} {%- macro hierarchical_list(data,hierarchy,region) %}
	<ol class="dd-list">
		{%- for key, value in hierarchy %}
		<li data-id="{{ key }}" class="dd-item dd2-item">
			<div class="dd-handle dd2-handle">
				<i class="glyphicon glyphicon-move bigger-130"></i> <i
					class="glyphicon glyphicon-move pink bigger-125"></i>
			</div>
			<div class="dd2-content">
				{{ data[key]['name'] }} <a data-placement="top" data-rel="tooltip"
					data-original-title="删除" class="pull-right"
					data-target="#right_handle"
					href="{{ url(['for':'adminRegionBlockDelete','block':key,'region':region]) }}"><i
					class="fa fa-fw fa-trash"></i></a> <a data-placement="top"
					data-rel="tooltip" data-original-title="编辑" class="pull-right"
					data-target="#right_handle"
					href="{{ url(['for':'adminRegionBlockEdit','block':key,'region':region,'contentModel':data[key]['contentModel']]) }}"><i
					class=" fa fa-fw
                           fa-pencil"></i></a>
			</div> {% if key != value %}{{ hierarchical_list(data,value,region)
			}}{% endif %}
		</li> {%- endfor %}
	</ol>
	{%- endmacro %}

    {% if is_array(data) AND is_array(hierarchy) %}
	{{ hierarchical_list(data,hierarchy,region) }}
	<button type="submit" class="btn btn-success btn-block save_sort">保存排序</button>
	{% else %}
	程序提供的数据合法或为空。
    {% endif %}
</div>
<script type="text/javascript">
(function($) {
    $('#{{ id }}').nestable();
    $('.dd-handle a').on('mousedown', function(e) {
        e.stopPropagation();
    });
    //$('[data-rel="tooltip"]').tooltip();
    $("#{{ id }} .save_sort").click(function() {
        var e = $("#{{ id }}");
        var list = e.length ? e : $(e.target),
            output = list.data('output');
        if (window.JSON) {
            var postJson = window.JSON.stringify(list.nestable('serialize'));
            $.post("{{ url }}", {
                rh: postJson
            }, function(data) {
                $("#right_handle").html(data);
                $.notice('提交成功');
            }, "html");
        } else {
            alert('您的浏览器不安全，不支持本系统.');
        }
    })
    $('[data-rel="tooltip"]').tooltip();

})(jQuery);
</script>
